package net.zoneland.knowledge.utils;

import java.util.Date;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;

import net.zoneland.knowledge.constant.Constants;
import net.zoneland.knowledge.enums.FootprintCategoryEnum;
import net.zoneland.knowledge.enums.QueStyleEnum;
import net.zoneland.knowledge.enums.VisitTypeEnum;
import net.zoneland.knowledge.model.LoginUserPojo;
import net.zoneland.knowledge.model.QaRecordsV2;
import net.zoneland.knowledge.service.bean.FootprintBaseQueryVO;

/**
 * @version v1.0
 * @ClassName: FootprintUtils
 * @Description: 我的足迹
 * @Author: 罗松
 * @Date: 2022/1/10 10:52
 */
public final class FootprintUtils {

    private FootprintUtils() {

    }

    /**
     * 获取足迹对象实体类。
     * @param categoryEnum 足迹类型枚举
     * @param queryVo 查询参数
     * @param request request
     * @return 足迹对象
     */
    public static QaRecordsV2 getQaRecordsV2(final FootprintCategoryEnum categoryEnum,
                                             final FootprintBaseQueryVO queryVo,
                                             final HttpServletRequest request) {
        QaRecordsV2 qaRecordsV2 = new QaRecordsV2();
        // 足迹类型
        qaRecordsV2.setCategory(Constants.STRING_SPLIT + categoryEnum.getCategory() + Constants.STRING_SPLIT);
        // 一级栏目
        qaRecordsV2.setLevel1Title(categoryEnum.getLevel1Title());
        // 问答类别固定为 1
        qaRecordsV2.setQueStyle(QueStyleEnum.PROFESSION_TALK.getValue());
        qaRecordsV2.setCreateTime(new Date());
        // 最终答案固定为 0
        qaRecordsV2.setFinalAnswer(0);
        // 操作设备
        qaRecordsV2.setVisitType(VisitTypeEnum.MOA);
        String visitType = request.getHeader("visit_type");
        if (StringUtils.isNotBlank(visitType)) {
            VisitTypeEnum typeEnum = VisitTypeEnum.getByValue(visitType);
            if (typeEnum != null) {
                qaRecordsV2.setVisitType(typeEnum);
            }
        }
        // 问题内容
        Assert.isTrue(StringUtils.isNotBlank(queryVo.getQueryText()), "问题内容不能为空");
        qaRecordsV2.setQueryText(queryVo.getQueryText());
        // 问答会话ID
        if (StringUtils.isNotBlank(queryVo.getTalkSessionId())) {
            qaRecordsV2.setSessionId(queryVo.getTalkSessionId());
        } else {
            qaRecordsV2.setSessionId(UUID.randomUUID().toString());
        }
        // 查询方式
        qaRecordsV2.setClickSearch(queryVo.getClickSearch() == null ? 1 : queryVo.getClickSearch());
        //获取当前登录人信息
        LoginUserPojo user = LoginUtils.getCurrentUserByToken(request.getHeader(Constants.TOKEN_NAME));
        Assert.notNull(user, "用户未登录或登录信息不存在");
        // 提问人信息
        qaRecordsV2.setReqId(user.getUserID());
        qaRecordsV2.setReqName(user.getUserName());
        qaRecordsV2.setCmpDn(user.getCompanyDN());
        qaRecordsV2.setDeptDn(user.getDepartmentDN());

        qaRecordsV2.setId(UUID.randomUUID().toString());
        return qaRecordsV2;
    }
}
